<?php
/**
 * 参数设置
 * @param unknown $add
 */
function SetDb($add) {
	global $db_password, $set_password, $db_dbname;
	if (empty ( $add ['outtime'] )) {
		$add ['outtime'] = 60;
	}
	// 修改密码
	if (empty ( $add ['dbpassword'] )) {
		$add ['dbpassword'] = $db_password;
	} elseif ($add ['dbpassword'] == 'null') {
		$add ['dbpassword'] = '';
	}
	$dblocalhost = $add ['dbhost'];
	// 端口
	if ($add ['dbport']) {
		$dblocalhost .= ":" . $add ['dbport'];
	}
	$link = @mysql_connect ( $dblocalhost, $add ['dbusername'], $add ['dbpassword'] );
	if (empty ( $link )) {
		printerror ( "FailDbuser", "history.go(-1)" );
	}
	// 默认数据库
	if ($add ['dbname']) {
		if ($add ['dbname'] != $db_dbname) {
			$usedb = @mysql_query ( "use `" . $add ['dbname'] . "`" );
			if (! $usedb) {
				printerror ( "NotDb", "history.go(-1)" );
			}
		}
	}
	// mysql版本
	if ($add ['mysqlver'] == 'auto') {
		$add ['mysqlver'] = GetMysqlVerForDb ();
		if (empty ( $add ['mysqlver'] )) {
			printerror ( "NotAutoDbVer", "history.go(-1)" );
		}
	}
	// 修改密码
	if ($add ['adminpassword']) {
		$add ['adminpassword'] = md5 ( $add ['adminpassword'] );
		$a = "\$set_password=\"" . addslashes ( $add ['adminpassword'] ) . "\";\r\n";
	} else {
		$add ['adminpassword'] = $set_password;
	}
	// 目录
	if (empty ( $add ['sbakpath'] )) {
		$add ['sbakpath'] = "bdata";
	}
	if (! file_exists ( RepPathStr ( $add ['sbakpath'] ) )) {
		printerror ( "NotBakpath", "history.go(-1)" );
	}
	if (empty ( $add ['sbakzippath'] )) {
		$add ['sbakzippath'] = "zip";
	}
	if (! file_exists ( RepPathStr ( $add ['sbakzippath'] ) )) {
		printerror ( "NotZippath", "history.go(-1)" );
	}
	if (empty ( $add ['sebaklang'] )) {
		$add ['sebaklang'] = 'gb,gbk';
	}
	$langr = explode ( ',', $add ['sebaklang'] );
	$string = "<?php
if(!defined('InTBak'))
{
	exit();
}

//Database
\$db_version=\"" . addslashes ( $add ['mysqlver'] ) . "\";
\$db_server=\"" . addslashes ( $add ['dbhost'] ) . "\";
\$db_port=\"" . addslashes ( $add ['dbport'] ) . "\";
\$db_username=\"" . addslashes ( $add ['dbusername'] ) . "\";
\$db_password=\"" . addslashes ( $add ['dbpassword'] ) . "\";
\$db_dbname=\"" . addslashes ( $add ['dbname'] ) . "\";
\$baktbpre=\"" . addslashes ( $add ['sbaktbpre'] ) . "\";
\$db_char=\"" . addslashes ( $add ['dbchar'] ) . "\";

//USER
\$set_username=\"" . addslashes ( $add ['adminusername'] ) . "\";
\$set_password=\"" . addslashes ( $add ['adminpassword'] ) . "\";
\$set_loginauth=\"" . addslashes ( $add ['adminloginauth'] ) . "\";
\$set_loginrnd=\"" . addslashes ( $add ['adminloginrnd'] ) . "\";
\$set_outtime=\"" . addslashes ( $add ['outtime'] ) . "\";
\$set_loginkey=\"" . addslashes ( $add ['loginkey'] ) . "\";

//COOKIE
\$cookiedomain=\"" . addslashes ( $add ['ckdomain'] ) . "\";
\$cookiepath=\"" . addslashes ( $add ['ckpath'] ) . "\";
\$cookievarpre=\"" . addslashes ( $add ['ckvarpre'] ) . "\";

//BAK
\$bakpath=\"" . addslashes ( $add ['sbakpath'] ) . "\";
\$bakzippath=\"" . addslashes ( $add ['sbakzippath'] ) . "\";
\$filechmod=\"" . addslashes ( $add ['sfilechmod'] ) . "\";
\$phpsafemod=\"" . @addslashes ( $add ['sphpsafemod'] ) . "\";
\$php_outtime=\"" . addslashes ( $add ['sphp_outtime'] ) . "\";
\$limittype=\"" . @addslashes ( $add ['slimittype'] ) . "\";
\$canlistdb=\"" . @addslashes ( $add ['scanlistdb'] ) . "\";

//------------ SYSTEM ------------
HeaderIeChar();
?>";
	$filename = "include/config.php";
	WriteFiletext_n ( $filename, $string );
	printerror ( "SetDbSuccess", "set_db.php" );
}

/**
 * 修复表
 * 
 * @param unknown $tablename
 * @param unknown $dbname
 */
function Rep($tablename, $dbname) {
	global $dblink;
	$dbname = RepPostVar ( $dbname );
	$dblink->query ( "use `$dbname`" );
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeTb", "history.go(-1)" );
	}
	for($i = 0; $i < $count; $i ++) {
		$sql1 = $dblink->query ( "REPAIR TABLE `$tablename[$i]`;" );
	}
	printerror ( "RepairTbSuccess", "change_table.php?mydbname=$dbname" );
}

/**
 * 忧化表
 * 
 * @param unknown $tablename
 * @param unknown $dbname
 */
function Opi($tablename, $dbname) {
	global $dblink;
	$dbname = RepPostVar ( $dbname );
	$dblink->query ( "use `$dbname`" );
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeTb", "history.go(-1)" );
	}
	for($i = 0; $i < $count; $i ++) {
		$sql1 = $dblink->query ( "OPTIMIZE TABLE `$tablename[$i]`;" );
	}
	printerror ( "OptimizeTbSuccess", "change_table.php?mydbname=$dbname" );
}

/**
 * 删除数据表
 * 
 * @param unknown $tablename
 * @param unknown $dbname
 */
function Drop($tablename, $dbname) {
	global $dblink;
	$dbname = RepPostVar ( $dbname );
	$dblink->query ( "use `$dbname`" );
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeDelTb", "history.go(-1)" );
	}
	$a = "";
	$first = 1;
	for($i = 0; $i < $count; $i ++) {
		if (empty ( $first )) {
			$a .= ",";
		} else {
			$first = 0;
		}
		$a .= "`" . $tablename [$i] . "`";
	}
	$sql1 = $dblink->query ( "DROP TABLE IF EXISTS " . $a . ";" );
	printerror ( "DelTbSuccess", "change_table.php?mydbname=$dbname" );
}

/**
 * 删除数据库
 * 
 * @param unknown $dbname
 */
function DropDb($dbname) {
	global $dblink;
	$dbname = RepPostVar ( $dbname );
	if (empty ( $dbname )) {
		printerror ( "NotChangeDelDb", "history.go(-1)" );
	}
	$sql = $dblink->query ( "DROP DATABASE `$dbname`" );
	if ($sql) {
		printerror ( "DelDbSuccess", "change_db.php" );
	} else {
		printerror ( "DbError", "history.go(-1)" );
	}
}

/**
 * 建立数据库
 * 
 * @param unknown $dbname
 * @param string $dbchar
 */
function CreatDb($dbname, $dbchar = '') {
	global $dblink, $db_version;
	$dbname = RepPostVar ( $dbname );
	if (! trim ( $dbname )) {
		printerror ( "EmptyDbname", "history.go(-1)" );
	}
	$a = "";
	if ($dbchar && $db_version >= '4.1') {
		$a = " DEFAULT CHARACTER SET " . $dbchar;
	}
	$sql = $dblink->query ( "CREATE DATABASE IF NOT EXISTS `$dbname`" . $a );
	if ($sql) {
		printerror ( "CreateDbSuccess", "change_db.php" );
	} else {
		printerror ( "DbError", "history.go(-1)" );
	}
}

/**
 * 清空表
 * 
 * @param unknown $tablename
 * @param unknown $dbname
 */
function EmptyTable($tablename, $dbname) {
	global $dblink;
	$dbname = RepPostVar ( $dbname );
	$dblink->query ( "use `$dbname`" );
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeTb", "history.go(-1)" );
	}
	for($i = 0; $i < $count; $i ++) {
		$sql1 = $dblink->query ( "TRUNCATE `" . $tablename [$i] . "`;" );
	}
	printerror ( "TruncateTbSuccess", "change_table.php?mydbname=$dbname" );
}

/**
 * 批量替换表名
 * 
 * @param unknown $tablename
 * @param unknown $oldpre
 * @param unknown $newpre
 * @param unknown $dbname
 */
function ReplaceTable($tablename, $oldpre, $newpre, $dbname) {
	global $dblink;
	if (! $oldpre) {
		printerror ( "EmptyReplaceTablePre", "history.go(-1)" );
	}
	$dbname = RepPostVar ( $dbname );
	$dblink->query ( "use `$dbname`" );
	$count = count ( $tablename );
	if (empty ( $count )) {
		printerror ( "EmptyChangeTb", "history.go(-1)" );
	}
	for($i = 0; $i < $count; $i ++) {
		$newtbname = str_replace ( $oldpre, $newpre, $tablename [$i] );
		$dblink->query ( "ALTER TABLE `" . $tablename [$i] . "` RENAME `" . $newtbname . "`;" );
	}
	printerror ( "ReplaceTbSuccess", "change_table.php?mydbname=$dbname" );
}

/**
 * 保存设置
 * 
 * @param unknown $add
 */
function SaveSeting($add) {
	$savename = $add ['savename'];
	if (strstr ( $savename, '.' ) || strstr ( $savename, '/' ) || strstr ( $savename, "\\" )) {
		printerror ( "FailSetSavename", "history.go(-1)" );
	}
	$baktype = ( int ) $add ['baktype'];
	$filesize = ( int ) $add ['filesize'];
	$bakline = ( int ) $add ['bakline'];
	$autoauf = ( int ) $add ['autoauf'];
	$bakstru = ( int ) $add ['bakstru'];
	$bakstrufour = ( int ) $add ['bakstrufour'];
	$beover = ( int ) $add ['beover'];
	$add ['waitbaktime'] = ( int ) $add ['waitbaktime'];
	$bakdatatype = ( int ) $add ['bakdatatype'];
	// 表列表
	$tblist = "";
	$tablename = $add ['tablename'];
	$count = count ( $tablename );
	if ($count) {
		for($i = 0; $i < $count; $i ++) {
			$tblist .= $tablename [$i] . ",";
		}
		$tblist = "," . $tblist;
	}
	$str = "<?php
\$dbaktype=" . $baktype . ";
\$dfilesize=" . $filesize . ";
\$dbakline=" . $bakline . ";
\$dautoauf=" . $autoauf . ";
\$dbakstru=" . $bakstru . ";
\$dbakstrufour=" . $bakstrufour . ";
\$ddbchar='" . addslashes ( $add ['dbchar'] ) . "';
\$dmypath='" . addslashes ( $add ['mypath'] ) . "';
\$dreadme=\"" . addslashes ( stripSlashes ( $add ['readme'] ) ) . "\";
\$dautofield='" . addslashes ( $add ['autofield'] ) . "';
\$dtblist='" . addslashes ( $tblist ) . "';
\$dbeover=" . $beover . ";
\$dinsertf='" . addslashes ( $add ['insertf'] ) . "';
\$dmydbname='" . addslashes ( $add ['mydbname'] ) . "';
\$dkeyboard='" . addslashes ( $add ['keyboard'] ) . "';
\$dwaitbaktime='" . $add ['waitbaktime'] . "';
\$dbakdatatype=" . $bakdatatype . ";
?>";
	$file = "setsave/" . $savename;
	WriteFiletext_n ( $file, $str );
	printerror ( "SetSaveSuccess", "history.go(-1)" );
}

/**
 * 删除设置
 * 
 * @param unknown $add
 */
function DelSeting($add) {
	$savename = $add ['savename'];
	if (strstr ( $savename, '.' ) || strstr ( $savename, '/' ) || strstr ( $savename, "\\" )) {
		printerror ( "FailSetSavename", "history.go(-1)" );
	}
	$file = "setsave/" . $savename;
	DelFiletext ( $file );
	printerror ( "DelSaveSetSuccess", "list_setbak.php?mydbname=$add[mydbname]&change=$add[change]" );
}

/**
 * 删除备份目录
 * 
 * @param unknown $path
 */
function DelBakpath($path) {
	global $bakpath;
	if (strstr ( $path, ".." )) {
		printerror ( "NotChangeDelPath", "history.go(-1)" );
	}
	if (! trim ( $path )) {
		printerror ( "NotChangeDelPath", "history.go(-1)" );
	}
	$delpath = $bakpath . "/" . $path;
	if (! file_exists ( $delpath )) {
		printerror ( "DelPathNotExists", "history.go(-1)" );
	}
	$delpath = DelPath ( $delpath );
	printerror ( "DelPathSuccess", "change_path.php?change=" . $_GET ['change'] );
}

/**
 * 删除压缩包
 * 
 * @param unknown $file
 */
function DelZip($file) {
	global $bakzippath;
	if (strstr ( $file, ".." )) {
		printerror ( "FileNotExists", "history.go(-1)", 9 );
	}
	if (empty ( $file )) {
		printerror ( "FileNotExists", "history.go(-1)", 9 );
	}
	$filename = $bakzippath . "/" . $file;
	if (! file_exists ( $filename )) {
		printerror ( "FileNotExists", "history.go(-1)", 9 );
	}
	DelFiletext ( $filename );
	printerror ( "DelZipSuccess", "history.go(-1)", 9 );
}

/**
 * 执行SQL语句
 * 
 * @param unknown $add
 */
function DoExecSql($add) {
	global $dblink, $db_dbname, $db_version, $db_char;
	$query = $add ['query'];
	if (! $query) {
		printerror ( "EmptyRunSql", "history.go(-1)" );
	}
	// 数据库
	if ($add ['mydbname']) {
		$dblink->query ( "use `" . $add ['mydbname'] . "`" );
	}
	// 编码
	if ($add ['mydbchar']) {
		DoSetDbChar ( $add ['mydbchar'] );
	}
	$query = ClearAddsData ( $query );
	DoRunQuery ( $query, $add ['mydbchar'], $db_version );
	printerror ( "RunSqlSuccess", "do_sql.php" );
}

/**
 * 上传执行SQL
 * 
 * @param unknown $file
 * @param unknown $file_name
 * @param unknown $file_type
 * @param unknown $file_size
 * @param unknown $add
 */
function DoTranExecSql($file, $file_name, $file_type, $file_size, $add) {
	global $dblink, $db_dbname, $db_version, $db_char;
	if (! $file_name || ! $file_size) {
		printerror ( "NotChangeSQLFile", "history.go(-1)" );
	}
	$filetype = GetFiletype ( $file_name ); // 取得扩展名
	if ($filetype != ".sql") {
		printerror ( "NotTranSQLFile", "history.go(-1)" );
	}
	// 上传文件
	$newfile = 'tmp/uploadsql' . time () . '.sql';
	$cp = DoTranFile ( $file, $newfile );
	if (empty ( $cp )) {
		printerror ( "TranSQLFileFail", "history.go(-1)" );
	}
	$query = ReadFiletext ( $newfile );
	DelFiletext ( $newfile );
	if (! $query) {
		printerror ( "EmptyRunSql", "history.go(-1)" );
	}
	// 数据库
	if ($add ['mydbname']) {
		$dblink->query ( "use `" . $add ['mydbname'] . "`" );
	}
	// 编码
	if ($add ['mydbchar']) {
		DoSetDbChar ( $add ['mydbchar'] );
	}
	DoRunQuery ( $query, $add ['mydbchar'], $db_version );
	printerror ( "RunSqlSuccess", "do_sql.php" );
}

/**
 * 替换文件内容
 * 
 * @param unknown $add
 */
function RepPathFiletext($add) {
	global $bakpath;
	$mypath = trim ( $add ['mypath'] );
	$oldword = ClearAddsData ( $add ['oldword'] );
	$newword = ClearAddsData ( $add ['newword'] );
	$dozz = ( int ) $add ['dozz'];
	if (empty ( $oldword ) || empty ( $mypath )) {
		printerror ( "EmptyRepPathFiletext", "history.go(-1)" );
	}
	if (strstr ( $mypath, ".." )) {
		printerror ( "NotChangeRepPathFiletext", "history.go(-1)" );
	}
	$path = $bakpath . "/" . $mypath;
	if (! file_exists ( $path )) {
		printerror ( "PathNotExists", "history.go(-1)" );
	}
	$hand = @opendir ( $path );
	while ( ! ! $file = @readdir ( $hand ) ) {
		$filename = $path . "/" . $file;
		if ($file != "." && $file != ".." && is_file ( $filename )) {
			$value = ReadFiletext ( $filename );
			if ($dozz) {
				$newvalue = DoRepFiletextZz ( $oldword, $newword, $value );
			} else {
				if (! stristr ( $value, $oldword )) {
					continue;
				}
				$newvalue = str_replace ( $oldword, $newword, $value );
			}
			WriteFiletext_n ( $filename, $newvalue );
		}
	}
	printerror ( "RepPathFiletextSuccess", "rep_filetext.php" );
}